package product;

import java.awt.EventQueue;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CodeInput extends JInternalFrame {
	Map<String, ProductData> hm = new HashMap<String, ProductData>();
	ProductData pd;
	boolean mdf;
	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	
	private JLabel lblNewLabel;
	private JLabel label;
	private JLabel label_1;
	private JTextField pcInput;
	private JTextField pnInput;
	private JTextField cosInput;
	private JButton btnAdd;
	
	
	public CodeInput() {
		super("CodeInput",true,true,false,false);
				
		setBounds(100, 100, 239, 158);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1_2());
		getContentPane().add(getPcInput());
		getContentPane().add(getPnInput());
		getContentPane().add(getCosInput());
		getContentPane().add(getBtnAdd());
	}
	public void addCode(){
		try{
			String s = pcInput.getText();				
			Set<String> st = hm.keySet();	
			Iterator<String> it = st.iterator();
			
			while(it.hasNext()){
				pd = hm.get(it.next());
				if(pd.getpCode().equals(s)){// 중복 코드저장으로 인한 데이터 초기화 방지
					JOptionPane.showMessageDialog(null, "해당 제품 코드는 이미 등록되어 있습니다.");
					pcInput.setText("");
					pnInput.setText("");
					cosInput.setText("");
					pcInput.requestFocus();
					return;
				}
			}
			
			if(pcInput.getText().trim().equals("") 
					|| pnInput.getText().trim().equals("") 
					|| cosInput.getText().trim().equals("")){
				JOptionPane.showMessageDialog(null, "빈칸을 모두 채워주세요");
				return;
			}else{
				ProductData pd = new ProductData(pcInput.getText(), pnInput.getText(), Integer.parseInt(cosInput.getText()));
				hm.put(pcInput.getText(), pd);
				JOptionPane.showMessageDialog(null, "데이터가 저장되었습니다");
				pcInput.setText("");
				pnInput.setText("");
				cosInput.setText("");
				pcInput.requestFocus();
				mdf=true; // 수정여부
			}
		}catch(NumberFormatException nfe){
			JOptionPane.showMessageDialog(null, "단가는 숫자만 입력해주세요.");
		}
	}
	public void setD(Map<String, ProductData> map, boolean a){
		this.hm = map;
		this.mdf=a;
	}
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("제품코드");
			lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel.setBounds(12, 10, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("제품명");
			label.setHorizontalAlignment(SwingConstants.RIGHT);
			label.setBounds(12, 35, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1_2() {
		if (label_1 == null) {
			label_1 = new JLabel("단가");
			label_1.setHorizontalAlignment(SwingConstants.RIGHT);
			label_1.setBounds(12, 60, 57, 15);
		}
		return label_1;
	}
	private JTextField getPcInput() {
		if (pcInput == null) {
			pcInput = new JTextField();
			pcInput.setBounds(81, 7, 116, 21);
			pcInput.setColumns(10);
		}
		return pcInput;
	}
	private JTextField getPnInput() {
		if (pnInput == null) {
			pnInput = new JTextField();
			pnInput.setColumns(10);
			pnInput.setBounds(81, 32, 116, 21);
		}
		return pnInput;
	}
	private JTextField getCosInput() {
		if (cosInput == null) {
			cosInput = new JTextField();
			cosInput.setColumns(10);
			cosInput.setBounds(81, 57, 116, 21);
		}
		return cosInput;
	}
	private JButton getBtnAdd() {
		if (btnAdd == null) {
			btnAdd = new JButton("추가");
			btnAdd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					addCode();					
				}
			});
			btnAdd.setBounds(81, 88, 116, 23);
		}
		return btnAdd;
	}
}
